﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender;
using Seagull.BarTender.Print;
using System.Windows.Forms;

namespace POSPlus.PrintManager
{
	public class SpecialInfoWriter : EzbtWriter
	{
		private string _target;
		private List<PrintJob> _jobNames;
		private string _connectionString;
		private List<SpecialPrintInfo> _printInfo;
		private string _labelLocation;

		public override string Target
		{
			get
			{
				return _target;
			}
			set
			{
				_target = value;
			}
		}

		public override List<PrintJob> JobName
		{
			get
			{
				return _jobNames;
			}
			set
			{
				_jobNames = value;	
			}
		}

		public override string ConnectionString
		{
			get
			{
				return _connectionString;
			}
			set
			{
				_connectionString = value;
			}
		}

		public SpecialInfoWriter(string connectionString, string labelLocation, List<PrintJob> jobs, string target)
		{
			ConnectionString = connectionString;
			JobName = jobs;
			Target = target;
			_labelLocation = labelLocation;
		}

		public override void Write()
		{
			BarTenderTreeBuilder builder = new BarTenderTreeBuilder(Target, "");
			builder.LabelLocation = _labelLocation;
			builder.StartTextData();
			String fields = "\"Description\",\"Vendor\",\"CircleFile\",\"Cert_Code\",\"AvgDailyMovement\",\"Pack\",\"Size\",\"Unit_NP\"," +
				"\"UOM\",\"RegularUpcCheckbit\",\"RegularUpcNoCheckbit\",\"RegularKehe\",\"Authorized\",\"RegularPriceLine1\",\"RegularPriceLine2\"," +
				"\"RegularPriceLine3\",\"UPC\",\"LocAisle\",\"LocBlock\",\"LocSeq\",\"PrintDate\",\"BatchNo\"," +
				"\"SpecialPriceLine1\",\"SpecialPriceLine2\",\"SpecialPriceLine3\",\"SpecialBarcodeCheckbit\"," +
				"\"SpecialBarcodeNoCheckbit\",\"SpecialKehe\",\"WhereToBuy\",\"PalletNo\",\"Start_Date\",\"End_Date\"," +
				"\"USave\",\"Unit_SP\",\"Rehang\"\n";
			builder.AppendTextData(fields);
			for (int i = 0; i < _printInfo.Count; i++)
			{
				SpecialPrintInfo specialInfo = _printInfo[i];
				string data = "";
				data += "\"" + specialInfo.Description + "\",";
				data += "\"" + specialInfo.Vendor.ToString() + "\",";
				data += "\"" + CircleFile(specialInfo.Vendor) + "\",";
				data += "\"" + (String.IsNullOrEmpty(specialInfo.CertCode) ? "" : specialInfo.CertCode.ToString()) + "\",";
				data += "\"" + AvgDailyMovement(specialInfo.AvgDailyMovement) + "\",";
				data += "\"" + specialInfo.Pack.ToString() + "\",";
				data += "\"" + specialInfo.Size + "\",";
				data += "\"" + UnitNP(specialInfo.UnitNP) + "\",";
				data += "\"" + specialInfo.Uom + "\",";
				data += "\"" + RegularUpcCheckbit(specialInfo.Vendor, specialInfo.CertCode) + "\",";
				data += "\"" + RegularUpcNoCheckbit(specialInfo.Vendor, specialInfo.CertCode) + "\",";
				data += "\"" + RegularKehe(specialInfo.Vendor, specialInfo.Upc) + "\",";
				data += "\"" + Authorized(specialInfo.Hosted) + "\",";
				data += "\"" + PriceLine1(specialInfo.PriceMethod, specialInfo.NormalPrice, specialInfo.Quantity, specialInfo.GroupPrice) + "\",";
				data += "\"" + PriceLine2(specialInfo.PriceMethod, specialInfo.NormalPrice, specialInfo.Quantity, specialInfo.GroupPrice) + "\",";
				data += "\"" + PriceLine3(specialInfo.PriceMethod, specialInfo.NormalPrice, specialInfo.Quantity, specialInfo.GroupPrice) + "\",";
				data += "\"" + specialInfo.Upc + "\",";
				data += "\"" + specialInfo.LocAisle.ToString() + "\",";
				data += "\"" + specialInfo.LocBlock.ToString() + "\",";
				data += "\"" + specialInfo.LocSeq.ToString() + "\",";
				data += "\"" + DateTime.Now.ToString("MM/dd/yyyy") + "\",";
				data += "\"" + specialInfo.BatchNo + "\",";
				data += "\"" + PriceLine1(specialInfo.SpecialPriceMethod, specialInfo.SpecialPrice, specialInfo.SpecialQuantity, specialInfo.SpecialGroupPrice) + "\",";
				data += "\"" + PriceLine2(specialInfo.SpecialPriceMethod, specialInfo.SpecialPrice, specialInfo.SpecialQuantity, specialInfo.SpecialGroupPrice) + "\",";
				data += "\"" + PriceLine3(specialInfo.SpecialPriceMethod, specialInfo.SpecialPrice, specialInfo.SpecialQuantity, specialInfo.SpecialGroupPrice) + "\",";
				data += "\"" + SpecialBarcodeCheckbit(specialInfo.Vendor, specialInfo.CertCode, specialInfo.PalletCode) + "\",";
				data += "\"" + SpecialBarcodeNoCheckbit(specialInfo.Vendor, specialInfo.CertCode, specialInfo.PalletCode) + "\",";
				data += "\"" + SpecialKehe(specialInfo.Vendor, specialInfo.Upc) + "\",";
				data += "\"" + specialInfo.WhereToBuy + "\",";
				data += "\"" + PalletCode(specialInfo.PalletCode, specialInfo.CertCode) + "\",";
				data += "\"" + StartDate(specialInfo.StartDate, specialInfo.EndDate) + "\",";
				data += "\"" + EndDate(specialInfo.StartDate, specialInfo.EndDate, specialInfo.DealEndDate) + "\",";
				data += "\"" + USave(specialInfo.PriceMethod, specialInfo.NormalPrice, specialInfo.Quantity, specialInfo.GroupPrice,
					specialInfo.SpecialPriceMethod, specialInfo.SpecialPrice, specialInfo.SpecialQuantity, specialInfo.SpecialGroupPrice) + "\",";
				data += "\"" + UnitNP(specialInfo.UnitSP) + "\",";
				data += "\"" + specialInfo.Rehang + "\",";
                data += "\"" + RegularEANKehe(specialInfo.Vendor, specialInfo.Upc) + "\",";
                data += "\"" + SpecialEANKehe(specialInfo.Vendor, specialInfo.Upc) + "\"";
				if (i < _printInfo.Count - 1)
					data += "\n";

				builder.AppendTextData(data);
			}

			builder.CloseTextData();

			using (Engine engine = new Engine(true))
			{
				Messages msgs = null;
				engine.XMLScript(Target, XMLSourceType.ScriptFile, out msgs);

				foreach (Seagull.BarTender.Print.Message msg in msgs) 
					MessageBox.Show(msg.Text, msg.Source.ToString());

				engine.Stop();
			}
		}

		public override void GetInfo()
		{
			_printInfo = new List<SpecialPrintInfo>();
			foreach (PrintJob job in _jobNames)
			{
				if (job.Type[job.Type.Length - 1] == 'F')
					_printInfo.AddRange(SpecialPrintInfo.GetFuture(ConnectionString, job.JobName));
				else
					_printInfo.AddRange(SpecialPrintInfo.Get(ConnectionString, job.JobName));
			}

			_printInfo.Sort();
		}

		private string SpecialBarcodeCheckbit(int vendor,string certCode,string palletCode)
		{
			if (vendor == 1 || vendor == 351)
			{
				if (!string.IsNullOrEmpty(palletCode))
					return palletCode.ToString();
				else
					return certCode;
			}
			else
				return "";
		}

		private string SpecialBarcodeNoCheckbit(int vendor, string certCode, string palletCode)
		{
			if (vendor == 3)
			{
				if (!string.IsNullOrEmpty(palletCode))
					return palletCode.ToString();
				else
					return certCode;
			}
			else
				return "";
		}

		private string SpecialKehe(int vendor, string upc)
        {
            if (vendor == 56 && upc.Length - CountLeftZeros(upc) <= 10)
                return string.Format("{0}", upc.Substring(3, 10));
            else
                return "";
		}

        private string SpecialEANKehe(int vendor, string upc)
        {
            int nonZeros = upc.Length - CountLeftZeros(upc);
            if (vendor == 56 && nonZeros > 10 && nonZeros <= 12)
                return string.Format("{0}", upc.Substring(1, 12));
            else
                return "";
        }

		public string StartDate(DateTime startDate, DateTime endDate)
		{
			if (endDate > startDate + new TimeSpan(360, 0, 0, 0))
				return DateTime.Now.ToString("MM/dd/yy");
			else
				return startDate.ToString("MM/dd/yy");
		}

		public string EndDate(DateTime startDate, DateTime endDate, DateTime dealEndDate)
		{
			if (endDate.Year >= 2020)
			{
				return (DateTime.Now + new TimeSpan(180, 0, 0, 0)).ToString("MM/dd/yy");
			}
			else if (dealEndDate != default(DateTime))
			{
				return dealEndDate.ToString("MM/dd/yy");
			}
			else
			{
				return endDate.ToString("MM/dd/yy");
			}
		}

		public string USave(int priceMethod, decimal price, int quantity, decimal groupPrice,
			int specialPriceMethod, decimal specialPrice, double specialQuantity, decimal specialGroupPrice)
		{
			double regularUnitPrice, specialUnitPrice;

			switch (priceMethod)
			{
				case 0: regularUnitPrice = (double)price; break;
				case 1: 
				case 2: regularUnitPrice = (double)groupPrice / quantity; break;
				case 3:
				case 4: regularUnitPrice = (double)groupPrice; break;
				default: regularUnitPrice = (double)price; break;
			}

			switch (specialPriceMethod)
			{
				case 0: specialUnitPrice = (double)specialPrice; break;
				case 1:
				case 2: specialUnitPrice = (double)specialGroupPrice / specialQuantity; break;
				case 3:
				case 4: specialUnitPrice = (double)specialGroupPrice; break;
				default: specialUnitPrice = (double)specialPrice; break;
			}

			double uSave = (double)regularUnitPrice - (double)specialUnitPrice;

			if (uSave <= 0) return "";
			else
				return ConvertMoney(uSave);
		}

		public string PalletCode(string palletCode, string certCode)
		{
			if (!string.IsNullOrEmpty(palletCode))
				return "P#" + palletCode.ToString();
			else
				return "i#" + certCode;
		}
	}
}
